# coding: utf-8
import traceback

from flask import render_template, redirect, request, url_for, flash, \
    current_app
from flask_login import login_user, login_required, logout_user, current_user
from common.log import log
from flask import Blueprint

from xshare.model.post import Post

post = Blueprint('post', __name__)


@post.route('/list/', methods=['GET', 'POST'])
def post_list_page():
    try:
        page = request.args.get('page', 1, type=int)
        log.info(request.args)
        result = Post.query.filter_by(is_deleted=0).order_by(Post.create_time.desc())
        pagination = result.paginate(
            page, per_page=current_app.config['CLUB_PER_PAGE'],
            error_out=False)
        posts = pagination.items

        return render_template('admin/post/list.html',
                               posts=posts, pagination=pagination,
                               endpoint='club.post_list_page', page=page)
    except:
        log.info(traceback.format_exc())
        print(traceback.format_exc())
        return redirect(url_for("others.index"))


# @club.route('/logout')
# @login_required
# def logout():
#     logout_user()
#     flash(u'您已退出登陆。', 'success')
#     return redirect(url_for('index.login'))
#
#
@post.route('/create/')
def create():
    if current_user.is_authenticated:
        return render_template('admin/index.html')
    else:
        print(url_for('admin.login'))
        return redirect(url_for('admin.login'))
